﻿@using Microsoft.AspNetCore.Builder;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@using System.Globalization;

<RadzenCard Variant="Variant.Filled" Style="@string.Join(';', innerCssStyles)">
    <RadzenStack Orientation="Orientation.Horizontal" Gap="1rem" Class="rz-p-4">
        <RadzenImage Path="@picture" style="max-width: 60px;max-height: 60px;" />
        <RadzenStack Gap="0">
            <RadzenText style="color: inherit !important;" TextStyle="TextStyle.DisplayH6">@(Display == null ? ServerId : Display.Name)</RadzenText>
                @if (Display != null)
            {
                <RadzenText style="color: inherit !important;" TextStyle="TextStyle.Body1">@Display.Description</RadzenText>
            }
        </RadzenStack>
    </RadzenStack>
    @if(Credential != null)
    {
        <RadzenStack Class="rz-p-4" Gap="1rem">
            <RadzenRow>
                <RadzenColumn Size="6">
                    @Global.CredentialIdentifier
                </RadzenColumn>
                <RadzenColumn Size="6">
                    @Credential.CredentialId
                </RadzenColumn>
            </RadzenRow>
            <RadzenRow>
                <RadzenColumn Size="6">
                    @Global.IssueDateTime
                </RadzenColumn>
                <RadzenColumn Size="6">
                    @Credential.IssueDateTime
                </RadzenColumn>
            </RadzenRow>
            <RadzenRow>
                <RadzenColumn Size="6">
                    @Global.ExpirationDateTime
                </RadzenColumn>
                <RadzenColumn Size="6">
                    @(Credential.ExpirationDateTime == null ? "-" : Credential.ExpirationDateTime.ToString())
                </RadzenColumn>
            </RadzenRow>
            @foreach(var claim in Credential.Claims)
            {
                <RadzenRow>
                    <RadzenColumn Size="6">
                        @claim.Name
                    </RadzenColumn>
                    <RadzenColumn Size="6">
                        @claim.Value
                    </RadzenColumn>
                </RadzenRow>
            }
        </RadzenStack>        
    }
    <RadzenStack JustifyContent="JustifyContent.End" Orientation="Orientation.Horizontal">
        @if (ActionsContent != null)
        {
            @ActionsContent
        }
    </RadzenStack>
</RadzenCard>

@code {
    [Parameter]
    public RenderFragment ActionsContent { get; set; }
    [Parameter]
    public string ServerId { get; set; }
    [Parameter]
    public CredentialConfigurationTranslation Display { get; set; }
    [Parameter]
    public Credential Credential { get; set; }

    string picture { get; set; }
    string innerCssStyles { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (Display == null) return;
        picture = Display?.LogoUrl ?? "_content/SimpleIdServer.CredentialIssuer.Website/images/wallet.svg";
        var cssStyles = new List<string>
        {
            "overflow:hidden"
        };
        if (!string.IsNullOrWhiteSpace(Display?.TextColor))
            cssStyles.Add($"color:{Display.TextColor}");
        if (!string.IsNullOrWhiteSpace(Display?.BackgroundColor))
            cssStyles.Add($"background-color:{Display.BackgroundColor}");
        innerCssStyles = string.Join(";", cssStyles);
    }
}